/**
 * \class CKeepAliveThread
 * \brief This is a thread to periodically notify service's aliveness to the discovery service.
 * \version 0.3.2
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2007/10/8
 */
#ifndef CKeepAliveThreadH
#define CKeepAliveThreadH

#include <iostream>
#include <iterator>
#include <vector>
#include <list>
#include <algorithm>
#include <fstream>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdexcept>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <signal.h>

#include "common_space.h"
#include "thread/CThread.h"
#include "service/CServiceRepository.h"
#include "CDiscoveryProxy.h"

using namespace std;
//---------------------------------------------------------------------------
class CKeepAliveThread : public CThread {
private :
	string host;
	int port;

	CServiceRepository *container;

	int alive_max_time;
	int alive_min_time;
	int alive_time;

	int max_retry;
	int retry;

	//pthread_mutex_t tlock;

	void run();

protected :
	virtual void heartbeat();
	void monitor();

public :
	CKeepAliveThread(char *name);
	virtual ~CKeepAliveThread();

	void keepalive(int ttl);	
	int keepalive();

	void bind(char *host, int port, CServiceRepository *container);
};
//---------------------------------------------------------------------------
#endif
